﻿@inherits BaseItemComponent

<CascadingValue Value="this" IsFixed="true">
    @ChildContent
</CascadingValue>

@code {
    private IDictionary<string, object> iconStyle = new Dictionary<string, object>();
    private IDictionary<string, object> emphasis = new Dictionary<string, object>();

    /// <summary>
    /// 是否显示该工具
    /// </summary>
    [Parameter]
    public bool? show { get; set; }

    /// <summary>
    /// 数据视图
    /// </summary>
    [Parameter]
    public string? title { get; set; }

    /// <summary>
    /// 可以通过 'image://url' 设置为图片，其中 URL 为图片的链接，或者 dataURI
    /// </summary>
    [Parameter]
    public string? icon { get; set; }

    /// <summary>
    /// 是否不可编辑（只读）
    /// </summary>
    [Parameter]
    public bool? readOnly { get; set; }

    /// <summary>
    /// 自定义 dataView 展现函数
    /// </summary>
    [Parameter]
    public string? optionToContent { get; set; }

    /// <summary>
    /// 在使用 optionToContent 的情况下，如果支持数据编辑后的刷新，需要自行通过该函数实现组装 option 的逻辑
    /// </summary>
    [Parameter]
    public string? contentToOption { get; set; }

    /// <summary>
    /// 数据视图上有三个话术，默认是['数据视图', '关闭', '刷新']
    /// </summary>
    [Parameter]
    public string[]? lang { get; set; }

    /// <summary>
    /// 数据视图浮层背景色
    /// </summary>
    [Parameter]
    public string? backgroundColor { get; set; }

    /// <summary>
    /// 数据视图浮层文本输入区背景色
    /// </summary>
    [Parameter]
    public string? textareaColor { get; set; }

    /// <summary>
    /// 数据视图浮层文本输入区边框颜色
    /// </summary>
    [Parameter]
    public string? textareaBorderColor { get; set; }

    /// <summary>
    /// 文本颜色
    /// </summary>
    [Parameter]
    public string? textColor { get; set; }

    /// <summary>
    /// 按钮颜色
    /// </summary>
    [Parameter]
    public string? buttonColor { get; set; }

    /// <summary>
    /// 按钮文本颜色
    /// </summary>
    [Parameter]
    public string? buttonTextColor { get; set; }

    public override void SetSetting(IDictionary<string, object> setting)
    {
        if (show.HasValue)
            FillSetting("show", show.Value);

        if (!string.IsNullOrEmpty(title))
            FillSetting("title", title);

        if (!string.IsNullOrEmpty(icon))
            FillSetting("icon", icon);

        if (readOnly.HasValue)
            FillSetting("readOnly", readOnly.Value);

        if (!string.IsNullOrEmpty(optionToContent))
            FillSetting("optionToContent", optionToContent);

        if (!string.IsNullOrEmpty(contentToOption))
            FillSetting("contentToOption", contentToOption);

        if (lang != null && lang.Length > 0)
            FillSetting("lang", lang);

        if (!string.IsNullOrEmpty(backgroundColor))
            FillSetting("backgroundColor", backgroundColor);

        if (!string.IsNullOrEmpty(textareaColor))
            FillSetting("textareaColor", textareaColor);

        if (!string.IsNullOrEmpty(textareaBorderColor))
            FillSetting("textareaBorderColor", textareaBorderColor);

        if (!string.IsNullOrEmpty(textColor))
            FillSetting("textColor", textColor);

        if (!string.IsNullOrEmpty(buttonColor))
            FillSetting("buttonColor", buttonColor);

        if (!string.IsNullOrEmpty(buttonTextColor))
            FillSetting("buttonTextColor", buttonTextColor);
    }

    protected override IDictionary<string, object> LoadSetting()
    {
        IDictionary<string, object>? setting = null;

        if (Base is Feature fecomponent)
        {
            fecomponent.SetDataView((dataView) =>
            {
                setting = dataView;
            });
        }

        return setting ?? new Dictionary<string, object>();
    }

    public void SetIconStyle(Action<IDictionary<string, object>> action)
    {
        FillSetting("iconStyle ", iconStyle);
        action?.Invoke(iconStyle);
    }

    public void SetEmphasis(Action<IDictionary<string, object>> action)
    {
        FillSetting("emphasis ", emphasis);
        action?.Invoke(emphasis);
    }
}
